/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Contacts.java
 *
 * Created on Sep 25, 2011, 6:03:28 AM
 */
package azhar.view;

import azhar.com.PopupMenuContact;
import azhar.com.TableOperation;
import azhar.persistance.Guru;
import azhar.persistance.Karyawan;
import azhar.persistance.Siswa;
import azhar.persistance.control.exceptions.NonexistentEntityException;
import azhar.view.control.AddGuru;
import azhar.view.control.AddKaryawan;
import azhar.view.control.AddSiswa;
import azhar.view.control.EditGuru;
import azhar.view.control.EditKaryawan;
import azhar.view.control.EditSiswa;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Al-Insan
 */
public class Contacts extends javax.swing.JPanel {

    private Main main;
    private DefaultTableModel model = new DefaultTableModel();
    private TableOperation tabOp;
    private ArrayList idGuru = new ArrayList();
    private ArrayList idKaryawan = new ArrayList();
    private ArrayList idSiswa = new ArrayList();

    /** Creates new form Contacts */
    public Contacts(Main m) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception ex) {
        }
        initComponents();
        tabOp = new TableOperation();
        main = m;
        initTable();
    }

    public void initTable() {
        for (int i = 0; i < main.dtGuru.length; i++) {
            String[] data = new String[main.dtGuru[i].size()];
            idGuru.add(main.dtGuru[i].get(0));
            for (int j = 1; j < data.length; j++) {
                data[j - 1] = (String) main.dtGuru[i].get(j);
            }
            setData("guru", data);
        }
        for (int i = 0; i < main.dtKaryawan.length; i++) {
            String[] data = new String[main.dtKaryawan[i].size()];
            idKaryawan.add(main.dtKaryawan[i].get(0));
            for (int j = 1; j < data.length; j++) {
                data[j - 1] = (String) main.dtKaryawan[i].get(j);
            }
            setData("karyawan", data);
        }
        for (int i = 0; i < main.dtSiswa.length; i++) {
            String[] data = new String[main.dtSiswa[i].size()];
            idSiswa.add(main.dtSiswa[i].get(0));
            for (int j = 1; j < data.length; j++) {
                data[j - 1] = (String) main.dtSiswa[i].get(j);
            }
            setData("siswa", data);
        }
    }

    public TableModel getGuruModel() {
        return guruTable.getModel();
    }

    public TableModel getSiswaModel() {
        return muridTable.getModel();
    }

    public TableModel getKaryawanModel() {
        return karyawanTable.getModel();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabContacts = new javax.swing.JTabbedPane();
        panelGuru = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        guruTable = new javax.swing.JTable();
        panelKaryawan = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        karyawanTable = new javax.swing.JTable();
        panelMurid = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jPanel8 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        muridTable = new javax.swing.JTable();

        setPreferredSize(new java.awt.Dimension(701, 333));
        setLayout(new java.awt.CardLayout());

        tabContacts.setTabPlacement(javax.swing.JTabbedPane.LEFT);

        jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jButton1.setText("Delete");
        jButton1.setOpaque(false);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel3.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 7, -1, -1));

        jButton2.setText("Add");
        jButton2.setOpaque(false);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel3.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 7, -1, -1));

        jButton3.setText("Edit");
        jButton3.setOpaque(false);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel3.add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 7, -1, -1));

        jPanel4.setLayout(new java.awt.CardLayout());

        guruTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "NIP", "Nama", "Alamat", "No HP", "Jenjang"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        guruTable.getTableHeader().setReorderingAllowed(false);
        guruTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                guruTableMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(guruTable);
        guruTable.getColumnModel().getColumn(0).setResizable(false);
        guruTable.getColumnModel().getColumn(0).setPreferredWidth(20);
        guruTable.getColumnModel().getColumn(1).setResizable(false);
        guruTable.getColumnModel().getColumn(2).setResizable(false);
        guruTable.getColumnModel().getColumn(3).setResizable(false);
        guruTable.getColumnModel().getColumn(4).setResizable(false);
        guruTable.getAccessibleContext().setAccessibleName("guru");

        jPanel4.add(jScrollPane1, "card2");

        javax.swing.GroupLayout panelGuruLayout = new javax.swing.GroupLayout(panelGuru);
        panelGuru.setLayout(panelGuruLayout);
        panelGuruLayout.setHorizontalGroup(
            panelGuruLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, 648, Short.MAX_VALUE)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 648, Short.MAX_VALUE)
        );
        panelGuruLayout.setVerticalGroup(
            panelGuruLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelGuruLayout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE))
        );

        tabContacts.addTab("Guru", panelGuru);

        jPanel5.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jButton4.setText("Delete");
        jButton4.setOpaque(false);
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jPanel5.add(jButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 7, -1, -1));

        jButton5.setText("Add");
        jButton5.setOpaque(false);
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        jPanel5.add(jButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 7, -1, -1));

        jButton6.setText("Edit");
        jButton6.setOpaque(false);
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });
        jPanel5.add(jButton6, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 7, -1, -1));

        jPanel6.setLayout(new java.awt.CardLayout());

        karyawanTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nama", "No HP", "Alamat"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        karyawanTable.getTableHeader().setReorderingAllowed(false);
        karyawanTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                karyawanTableMouseReleased(evt);
            }
        });
        jScrollPane2.setViewportView(karyawanTable);
        karyawanTable.getColumnModel().getColumn(0).setResizable(false);
        karyawanTable.getColumnModel().getColumn(1).setResizable(false);
        karyawanTable.getColumnModel().getColumn(2).setResizable(false);
        karyawanTable.getAccessibleContext().setAccessibleName("karyawan");

        jPanel6.add(jScrollPane2, "card2");

        javax.swing.GroupLayout panelKaryawanLayout = new javax.swing.GroupLayout(panelKaryawan);
        panelKaryawan.setLayout(panelKaryawanLayout);
        panelKaryawanLayout.setHorizontalGroup(
            panelKaryawanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, 648, Short.MAX_VALUE)
            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, 648, Short.MAX_VALUE)
        );
        panelKaryawanLayout.setVerticalGroup(
            panelKaryawanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelKaryawanLayout.createSequentialGroup()
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE))
        );

        tabContacts.addTab("Karyawan", panelKaryawan);

        jPanel7.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jButton7.setText("Delete");
        jButton7.setOpaque(false);
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });
        jPanel7.add(jButton7, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 7, -1, -1));

        jButton8.setText("Add");
        jButton8.setOpaque(false);
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });
        jPanel7.add(jButton8, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 7, -1, -1));

        jButton9.setText("Edit");
        jButton9.setOpaque(false);
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });
        jPanel7.add(jButton9, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 7, -1, -1));

        jPanel8.setLayout(new java.awt.CardLayout());

        muridTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "NIS", "Nama", "Kelas", "Alamat", "Orang Tua / Wali", "No HP"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        muridTable.getTableHeader().setReorderingAllowed(false);
        muridTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                muridTableMouseReleased(evt);
            }
        });
        jScrollPane3.setViewportView(muridTable);
        muridTable.getColumnModel().getColumn(0).setResizable(false);
        muridTable.getColumnModel().getColumn(0).setPreferredWidth(20);
        muridTable.getColumnModel().getColumn(1).setResizable(false);
        muridTable.getColumnModel().getColumn(1).setPreferredWidth(100);
        muridTable.getColumnModel().getColumn(2).setResizable(false);
        muridTable.getColumnModel().getColumn(2).setPreferredWidth(25);
        muridTable.getColumnModel().getColumn(3).setResizable(false);
        muridTable.getColumnModel().getColumn(4).setResizable(false);
        muridTable.getColumnModel().getColumn(5).setResizable(false);
        muridTable.getAccessibleContext().setAccessibleName("siswa");

        jPanel8.add(jScrollPane3, "card2");

        javax.swing.GroupLayout panelMuridLayout = new javax.swing.GroupLayout(panelMurid);
        panelMurid.setLayout(panelMuridLayout);
        panelMuridLayout.setHorizontalGroup(
            panelMuridLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel8, javax.swing.GroupLayout.DEFAULT_SIZE, 648, Short.MAX_VALUE)
            .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, 648, Short.MAX_VALUE)
        );
        panelMuridLayout.setVerticalGroup(
            panelMuridLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelMuridLayout.createSequentialGroup()
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel8, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE))
        );

        tabContacts.addTab("Siswa", panelMurid);

        add(tabContacts, "card2");
        tabContacts.getAccessibleContext().setAccessibleName("tab");
    }// </editor-fold>//GEN-END:initComponents

    private void guruTableMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_guruTableMouseReleased
        if (evt.getButton() == evt.BUTTON3) {
            if (guruTable.getSelectedRow() != -1) {
                if (evt.isPopupTrigger()) {
                    new PopupMenuContact(guruTable, main, this).show(evt.getComponent(), evt.getX(), evt.getY());
                }
            }
        }
    }//GEN-LAST:event_guruTableMouseReleased

    private void karyawanTableMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_karyawanTableMouseReleased
        if (evt.getButton() == evt.BUTTON3) {
            if (karyawanTable.getSelectedRow() != -1) {
                if (evt.isPopupTrigger()) {
                    new PopupMenuContact(karyawanTable, main, this).show(evt.getComponent(), evt.getX(), evt.getY());
                }
            }
        }
    }//GEN-LAST:event_karyawanTableMouseReleased

    private void muridTableMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_muridTableMouseReleased
        if (evt.getButton() == evt.BUTTON3) {
            if (muridTable.getSelectedRow() != -1) {
                if (evt.isPopupTrigger()) {
                    new PopupMenuContact(muridTable, main, this).show(evt.getComponent(), evt.getX(), evt.getY());
                }
            }
        }
    }//GEN-LAST:event_muridTableMouseReleased

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        new AddGuru(main, true, this).setVisible(true);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        new AddSiswa(main, true, this).setVisible(true);
    }//GEN-LAST:event_jButton8ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        new AddKaryawan(main, true, this).setVisible(true);
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        if (guruTable.getSelectedRowCount() > 0 && guruTable.getSelectedRowCount() < 2) {
            new EditGuru(main, true, this, guruTable, guruTable.getSelectedRow()).setVisible(true);
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        for (int i = guruTable.getSelectedRows().length - 1; i >= 0; i--) {
            deleteData(guruTable.getAccessibleContext().getAccessibleName(), guruTable.getSelectedRows()[i]);
        }
        tabOp.deleteData(guruTable, guruTable.getSelectedRows());
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        if (karyawanTable.getSelectedRowCount() > 0 && karyawanTable.getSelectedRowCount() < 2) {
            new EditKaryawan(main, true, this, karyawanTable, karyawanTable.getSelectedRow()).setVisible(true);
        }
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        for (int i = karyawanTable.getSelectedRows().length - 1; i >= 0; i--) {
            deleteData(karyawanTable.getAccessibleContext().getAccessibleName(), karyawanTable.getSelectedRows()[i]);
        }
        tabOp.deleteData(karyawanTable, karyawanTable.getSelectedRows());
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        if (muridTable.getSelectedRowCount() > 0 && muridTable.getSelectedRowCount() < 2) {
            new EditSiswa(main, true, this, muridTable, muridTable.getSelectedRow()).setVisible(true);
        }
    }//GEN-LAST:event_jButton9ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        for (int i = muridTable.getSelectedRows().length - 1; i >= 0; i--) {
            deleteData(muridTable.getAccessibleContext().getAccessibleName(), muridTable.getSelectedRows()[i]);
        }
        tabOp.deleteData(muridTable, muridTable.getSelectedRows());
    }//GEN-LAST:event_jButton7ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable guruTable;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable karyawanTable;
    private javax.swing.JTable muridTable;
    private javax.swing.JPanel panelGuru;
    private javax.swing.JPanel panelKaryawan;
    private javax.swing.JPanel panelMurid;
    private javax.swing.JTabbedPane tabContacts;
    // End of variables declaration//GEN-END:variables

    public void addData(String key, String[] data) {
        if (key.equalsIgnoreCase("guru")) {
            String[] d = {data[0], data[1], data[2], data[3], main.dbControl.jenjang.findJenjang(Integer.parseInt(data[4])).getNamaJenjang()};
            tabOp.insertAtLast(guruTable, d);
            main.dbControl.guru.create(new Guru(0, data[0], data[1], data[2], data[3], Integer.parseInt(data[4])));
            idGuru.add(main.dbControl.getLastIdGuru());
        } else if (key.equalsIgnoreCase("karyawan")) {
            tabOp.insertAtLast(karyawanTable, data);
            main.dbControl.karyawan.create(new Karyawan(0, data[0], data[1], data[2]));
            idKaryawan.add(main.dbControl.getLastIdKaryawan());
        } else if (key.equalsIgnoreCase("siswa")) {
            String[] d = {data[0], data[1], main.dbControl.kelas.findKelas(Integer.parseInt(data[2])).getNamaKelas(), data[3], data[4], data[5]};
            tabOp.insertAtLast(muridTable, d);
            int id = Integer.parseInt(data[2]);
            main.dbControl.siswa.create(new Siswa(0, d[0], d[1], id, d[3], d[4], d[5]));
            idSiswa.add(main.dbControl.getLastIdSiswa());
        }
    }

    public void setData(String key, String[] data) {
        if (key.equalsIgnoreCase("guru")) {
            tabOp.insertData(guruTable, data);
        } else if (key.equalsIgnoreCase("karyawan")) {
            tabOp.insertData(karyawanTable, data);
        } else if (key.equalsIgnoreCase("siswa")) {
            tabOp.insertData(muridTable, data);
        }
    }

    public void editData(String key, String[] data, int row) {
        if (key.equalsIgnoreCase("guru")) {
            String n = (String) idGuru.get(row);
            try {
                String[] d = {data[0], data[1], data[2], data[3], main.dbControl.jenjang.findJenjang(Integer.parseInt(data[4])).getNamaJenjang()};
                tabOp.updateData(guruTable, d, row);
                main.dbControl.guru.edit(new Guru(Integer.parseInt(n), data[0], data[1], data[2], data[3], Integer.parseInt(data[4])));
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(main, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
            }
        } else if (key.equalsIgnoreCase("karyawan")) {
            try {
                String n = (String) idKaryawan.get(row);
                tabOp.updateData(karyawanTable, data, row);
                main.dbControl.karyawan.edit(new Karyawan(Integer.parseInt(n), data[0], data[1], data[2]));
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(main, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
            }
        } else if (key.equalsIgnoreCase("siswa")) {
            try {
                String n = (String) idSiswa.get(row);
                String[] d = {data[0], data[1], main.dbControl.kelas.findKelas(Integer.parseInt(data[2])).getNamaKelas(), data[3], data[4], data[5]};
                tabOp.updateData(muridTable, d, row);
                int id = Integer.parseInt(data[2]);
                main.dbControl.siswa.edit(new Siswa(Integer.parseInt(n), d[0], d[1], id, d[3], d[4], d[5]));
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(main, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    public void deleteData(String key, int id) {
        System.out.println(id);
        if (key.equals("guru")) {
            String n = "" + idGuru.get(id);
            try {
                main.dbControl.guru.destroy(Integer.parseInt(n));
                idGuru.remove(id);
            } catch (NonexistentEntityException ex) {
                Logger.getLogger(Contacts.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (key.equals("karyawan")) {
            String n = "" + idKaryawan.get(id);
            try {
                main.dbControl.karyawan.destroy(Integer.parseInt(n));
                idKaryawan.remove(id);
            } catch (NonexistentEntityException ex) {
                Logger.getLogger(Contacts.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (key.equals("siswa")) {
            String n = "" + idSiswa.get(id);
            try {
                main.dbControl.siswa.destroy(Integer.parseInt(n));
                idSiswa.remove(id);
            } catch (NonexistentEntityException ex) {
                Logger.getLogger(Contacts.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
